﻿/*
 * Autore: Artur Tolstenco
 */

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using RecipesManager.Data;

namespace RecipesManager.MainControls.RecipeSubControls
{
    /// <summary>
    /// Interaction logic for StepSubControl.xaml
    /// </summary>
    public partial class StepSubControl : UserControl
    {
        internal readonly SubRecipeStepSubControl _subRecipeStepControl;
        internal readonly GenericStepSubControl _genericStepControl;

        StepFromRecipe _currentStep;
        string _currentRecipeName;

        public StepSubControl()
        {
            _subRecipeStepControl = new SubRecipeStepSubControl();
            _genericStepControl = new GenericStepSubControl();

            _subRecipeStepControl.cb_recipes.SelectionChanged += delegate
                                                                     {
                                                                         var curSubRecipeName = _subRecipeStepControl.cb_recipes.SelectedItem as string;
                                                                         if (curSubRecipeName == null)
                                                                             return;
                                                                         bt_salva.IsEnabled = IsModified();
                                                                     };
            _subRecipeStepControl.cb_servings.SelectionChanged += (sender, args) => bt_salva.IsEnabled = IsModified();
            _genericStepControl.tb_employed_resorces.TextChanged += (sender, args) => bt_salva.IsEnabled = IsModified();
            _genericStepControl.tb_step_description.TextChanged += (sender, args) => bt_salva.IsEnabled = IsModified();

            InitializeComponent();

            var max = Data.Properties.Settings.Default.MaxHours;
            var min = Data.Properties.Settings.Default.MinHours;
            var step = 1;

            for (var i = min; i <= max; i += step)
                cb_tempoOre.Items.Add(i);

            max = Data.Properties.Settings.Default.MaxMinutes;
            min = Data.Properties.Settings.Default.MinMinutes;
            step = 1;

            for (var i = min; i <= max; i += step)
                cb_tempoMin.Items.Add(i);
            

        }

        public string CurrentRecipeName
        {
            get { return _currentRecipeName; }
            set
            {
                _currentRecipeName = value;
            }
        }

        public StepFromRecipe CurrentStep
        {
            get { return _currentStep; }
            set
            {
                _currentStep = value;
                if (_currentStep != null)
                    SelectedId = _currentStep.UpdatedId;
                MyInitialize();
            }
        }

        public int SelectedId { get; set; }

        public bool AddBefore { get; set; }

        void MyInitialize()
        {

            var cbRecipes = _subRecipeStepControl.cb_recipes;
            cbRecipes.Items.Clear();
            foreach (var r in Data.Operations.Operations.GetAddableSubRecipes(_currentRecipeName))
                cbRecipes.Items.Add(r.Name);


            if (_currentStep == null)
            {
                tb_title.Text = Application.Current.FindResource("AddStepToRecipeTitle") + " " + _currentRecipeName;

                string whereToAdd = "";
                if (AddBefore)
                    if (SelectedId == -1)
                        whereToAdd += "prima del primo passo";
                    else
                        whereToAdd += "prima del passo " + (SelectedId + 1);
                else
                    if (SelectedId == -1)
                        whereToAdd += "dopo l'ultimo passo";
                    else
                        whereToAdd += "dopo il passo " + (SelectedId + 1);

                tb_title.Text += Environment.NewLine + "(" + whereToAdd + ")";

                bt_salva.Content = new Image { Source = new BitmapImage(new Uri("../../Icons/document-add.png", UriKind.Relative)) };
                bt_salva.ToolTip = Application.Current.FindResource("AddStepToRecipeTooltip");

                _genericStepControl.tb_step_description.Text = "";
                _genericStepControl.tb_employed_resorces.Text = "";
                cb_tempoMin.SelectedIndex = cb_tempoOre.SelectedIndex = -1;
                _subRecipeStepControl.cb_recipes.SelectedIndex = _subRecipeStepControl.cb_servings.SelectedIndex = -1;
                
                rb_generic_step.IsChecked = true;
                cb_tempoMin.IsEnabled = cb_tempoOre.IsEnabled = true;
            } 
            else
            {
                tb_title.Text = Application.Current.FindResource("EditStepFromRecipeTitle") + " " + _currentRecipeName;
                bt_salva.Content = new Image { Source = new BitmapImage(new Uri("../../Icons/document-save.png", UriKind.Relative)) };
                bt_salva.ToolTip = Application.Current.FindResource("SaveButtonTooltip");


                cb_tempoOre.SelectedItem = _currentStep.StepHours;
                cb_tempoMin.SelectedItem = _currentStep.StepMinutes;

                if (_currentStep.Type == StepType.SubRecipe)
                {
                    _subRecipeStepControl.cb_recipes.SelectedItem = _currentStep.SubRecipeName;
                    _subRecipeStepControl.cb_servings.SelectedItem = _currentStep.Servings;
                    _genericStepControl.tb_step_description.Text = _genericStepControl.tb_employed_resorces.Text = "";
                    

                    rb_subrecipe_step.IsChecked = true;
                }
                else
                {
                    _genericStepControl.tb_step_description.Text = _currentStep.Description;
                    _genericStepControl.tb_employed_resorces.Text = _currentStep.Resources;


                    rb_generic_step.IsChecked = true;
                }

            }

            bt_salva.IsEnabled = IsModified();

        }

        private void Rb_Generic_Step_Checked(object sender, RoutedEventArgs e)
        {
            var rb = sender as RadioButton;
            if (rb == null)
                return;
            if (rb.Equals(rb_generic_step))
            {
                if (rb_generic_step.IsChecked.HasValue && rb_generic_step.IsChecked.Value)
                {
                    stepContentPane.Content = _genericStepControl;
                }

                bt_salva.IsEnabled = IsModified();
            }
            else if (rb.Equals(rb_subrecipe_step))
            {
                if (rb_subrecipe_step.IsChecked.HasValue && rb_subrecipe_step.IsChecked.Value)
                {
                    stepContentPane.Content = _subRecipeStepControl;

                    bt_salva.IsEnabled = IsModified();
                }
            }
        }

        public bool IsModified()
        {
            if (_currentStep == null)
            {
                if (cb_tempoOre.SelectedIndex != -1 || cb_tempoMin.SelectedIndex != -1)
                    return true;
                if (rb_generic_step.IsChecked.HasValue && rb_generic_step.IsChecked.Value)
                    return !_genericStepControl.tb_step_description.Text.Equals("") ||
                           !_genericStepControl.tb_employed_resorces.Text.Equals("");
                return _subRecipeStepControl.cb_recipes.SelectedIndex != -1 ||
                       _subRecipeStepControl.cb_servings.SelectedIndex != -1;
            }

            if (_currentStep.Type == StepType.Generic)
            {
                var resource = _currentStep.Resources ?? "";
                if (!rb_generic_step.IsChecked.HasValue || rb_generic_step.IsChecked.Value != true ||
                    !_genericStepControl.tb_step_description.Text.ToLower().Equals(_currentStep.Description.ToLower()) ||
                    !_genericStepControl.tb_employed_resorces.Text.ToLower().Equals(resource.ToLower()))
                    return true;
            }
            else
            {
                var selectedRecipe = _subRecipeStepControl.cb_recipes.SelectedItem as string;
                if (!rb_subrecipe_step.IsChecked.HasValue || rb_subrecipe_step.IsChecked.Value != true ||
                    selectedRecipe == null ||
                    !selectedRecipe.Equals(_currentStep.SubRecipeName))
                    return true;

                var selectedServing = _subRecipeStepControl.cb_servings.SelectedItem as int?;

                if (_currentStep.Servings.HasValue)
                {
                    if (!selectedServing.HasValue || _currentStep.Servings.Value != selectedServing.Value)
                        return true;
                }
                else
                {
                    if (selectedServing.HasValue)
                        return true;
                }
            }

            var curTime = cb_tempoOre.SelectedItem as int?;
            if (_currentStep.StepHours.HasValue)
            {
                if (!curTime.HasValue || curTime.Value != _currentStep.StepHours.Value)
                    return true;
            }
            else
            {
                if (curTime.HasValue)
                    return true;
            }

            curTime = cb_tempoMin.SelectedItem as int?;
            if (_currentStep.StepMinutes.HasValue)
            {
                if (!curTime.HasValue || curTime.Value != _currentStep.StepMinutes.Value)
                    return true;
            }
            else
            {
                if (curTime.HasValue)
                    return true;
            }

            return false;
        }

        private void Cb_TempoOre_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            bt_salva.IsEnabled = IsModified();
        }
    }
}
